package wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.impl.IAuthService;
import wechat.util.StreamUtil;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@RequestMapping("/auth")
public class AuthController {

	private Logger log = LoggerFactory.getLogger(AuthController.class);

	@Autowired
	private IAuthService authService;

	@RequestMapping(value = "/getUserId/{appid}", method = RequestMethod.GET)
	@ResponseBody
	public String getUserId(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "code") String code) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String userid = "";
		try {
			userid = authService.getUserId(instanceResource, code);
			log.info("AuthController -- getUserId  userid=" + userid);
		} catch (Exception e) {
			log.error("AuthController -- getUserId  获取授权失败",e);
			e.printStackTrace();
		}
		JSONObject result = new JSONObject();
		result.put("userid", userid);

		return result.toJSONString();
	}

	@RequestMapping(value = "/getOauth2URL/{appid}", method = RequestMethod.GET)
	@ResponseBody
	public String getOauth2URL(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "state",required = false) String state) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String oauth2URL = "";
		try {
			oauth2URL = authService.getOauth2URL(instanceResource, state);
			log.info("AuthController -- getOauth2URL  userid=" + oauth2URL);
		} catch (Exception e) {
			log.error("AuthController -- getOauth2URL  获取授权失败",e);
			e.printStackTrace();
		}
		JSONObject result = new JSONObject();
		result.put("oauth2URL", oauth2URL);

		return result.toJSONString();

	}

	@RequestMapping(value = "/getOpenidByUserid/{appid}", method = RequestMethod.GET)
	@ResponseBody
	public String getOpenidByUserid(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "userid") String userid,
			@RequestParam(value = "agentid",required = false) String agentid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String openid = "";
		try {
			openid = authService.getOpenidByUserid(instanceResource, userid, agentid);
			log.info("AuthController -- getOpenidByUserid  openid=" + openid);
		} catch (Exception e) {
			log.error("AuthController -- getOpenidByUserid  获取授权失败",e);
			e.printStackTrace();
		}
		JSONObject result = new JSONObject();
		result.put("openid", openid);

		return result.toJSONString();

	}

	@RequestMapping(value = "/getUseridByOpenid/{appid}", method = RequestMethod.GET)
	@ResponseBody
	public String getUseridByOpenid(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid,
			@RequestParam(value = "openid") String openid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		String userid = "";
		try {
			userid = authService.getUseridByOpenid(instanceResource, openid);
			log.info("AuthController -- getUseridByOpenid  userid=" + userid);
		} catch (Exception e) {
			log.error("AuthController -- getUseridByOpenid  获取授权失败",e);
			e.printStackTrace();
		}
		JSONObject result = new JSONObject();
		result.put("userid", userid);

		return result.toJSONString();
	}
	
	
	
	/**
	 * 获取js签名
	 * 
	 *  数据格式：
	 * 
	 * 	  	{                                       
	 *		   "url": "http://www.exmam.com",                         
	 *		}
	 */
	@RequestMapping(value = "/getSignJSInfo/{appid}", method = RequestMethod.POST)
	@ResponseBody
	public String getSignJSInfo(HttpServletRequest request, 
			@PathVariable(value = "appid") String appid) {

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		
		InputStream input;
		String result = "";
		
		try {
			input = request.getInputStream();
			String str = StreamUtil.inputStreamToString(input);
			log.info("AuthController -- getSignJSInfo  str=" + str);
			
			JSONObject jsonParam = JSONObject.parseObject(str);
			result = authService.getSignJSInfo(instanceResource, jsonParam);
			
		} catch (IOException e) {
			log.error("AuthController -- getSignJSInfo  解析失败",e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error("AuthController -- getSignJSInfo  失败",e);
			e.printStackTrace();
		}

		return result;
	}

}
